<template>
    <h2>查询客户操作</h2>

    <el-table :data="custList" style="width: 100%">
        <el-table-column prop="id" key="cust" label="客户编号" width="180" />
        <el-table-column prop="custName" label="客户姓名" width="180" />
        <el-table-column prop="address" label="客户地址" />
        <el-table-column prop="phone" label="客户电话" />
        <el-table-column prop="custType" label="客户职业" />
        <el-table-column prop="level" label="客户等级" />
        <el-table-column fixed="right" label="操作">

            <template #default="scope">
                <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">
                    修改
                </el-button>

                <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination layout="prev, pager, next" :total="total" @current-change="getCustList" v-model:current-page="currentPage"/>

    <el-dialog v-model="dialogVisible" title="修改信息" width="500">
        <el-form-item label="客户姓名">
            <el-input v-model="custForm.name" />
        </el-form-item>
        <el-form-item label="地址">
            <el-input v-model="custForm.address" />
        </el-form-item>
        <el-form-item label="手机号">
            <el-input v-model="custForm.phone" />
        </el-form-item>
        <el-form-item label="客户类型">
            <el-select v-model="custForm.custType" placeholder="please select your type">
                <el-option label="职业1" value="zhiye1" />
                <el-option label="职业2" value="zhiye2" />
            </el-select>
        </el-form-item>
        <el-form-item label="客户等级" :rules="[
            { required: true, message: 'age is required' },
            { type: 'number', message: 'grade must be a number' },
        ]">
            <el-input v-model.number="custForm.level" type="text" autocomplete="off" />
        </el-form-item>

        <el-form-item label="历史总计">
            <el-input v-model="custForm.history" />
        </el-form-item>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="updateCust">
                    修改
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { reactive } from 'vue'
import axios from 'axios'
import { onMounted, ref } from 'vue';
const custList = ref([])
const total = ref()
const dialogVisible = ref(false)
const currentPage = ref(1)
function getCustList(current) {
    console.log(current)
    axios.get("http://localhost:8081/custList?pageNum=" + current).then((resp) => {
        console.log(resp.data)
        custList.value = resp.data.rows
        total.value = resp.data.total
    }).catch((error) => {
        console.log(error)
    })
}

const custForm = reactive({
    custName: '',
    address: '',
    phone: '',
    custType: '',
    level: 1,
    history: 0,

})

const handleClose = () => {
    ElMessageBox.confirm('Are you sure to close this dialog?')
        .then(() => {
            done()
        })
        .catch(() => {
            // catch error
        })
}

function handleEdit(index, row) {
    console.log(index, row)
    custForm.name = row.name
    custForm.address = row.address
    custForm.phone = row.phone
    custForm.level = row.level
    custForm.custType = row.custType
    custForm.id = row.id

    dialogVisible.value = true
}

function updateCust() {
    axios.post("http://localhost:8081/updateCust", custForm).then((resp) => {
        if (resp.data.code == 200) {
            ElMessage({message: resp.data.msg, type: 'success'})
            console.log("Page: " + currentPage.value)
            getCustList(currentPage.value)
        } else {
            ElMessage.error(resp.data.msg)
        }
        dialogVisible.value = false
    }).catch((error) => {
        console.log(error)
    })
}

function handleDelete(index, row) {
    console.log(index, row)
    axios.get("http://localhost:8081/deleteCust?id=" + row.id).then((resp) => {
        if (resp.data.code == 200) {
            ElMessage({message: resp.data.msg, type: 'success'})
            getCustList(currentPage.value)
        } else {
            ElMessage.error(resp.data.msg)
        }
    }).catch((error) => {
        console.log(error)
    })
}

onMounted(function () {
    getCustList(1)
})
</script>

<style scoped></style>